﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tosi
{
    class LightGroup
    {
        public LightControls[] Group;
        private Junction myJunction;
        //private double WaitTimeSum;
        //private double WaitTimeAvg;
        //private double JunctionCongestion;
        //private double StreetLoad;
        private double waitingCarsAvg;
        private double waitingTimeAvg;
        private double junctionCongestion;
        private double sum1;
        private double sum2;
        private double sum3;
        private double TOSI;

        public LightGroup(Junction J)
        {
            waitingCarsAvg = 0;
            waitingTimeAvg = 0;
            junctionCongestion = 0;
            sum1 = 0;
            sum2 = 0;
            sum3 = 0;
            TOSI = 0;
            myJunction = J;
            int NrOfLights = J.getInStreets().Count;
            Group = new LightControls[NrOfLights];
            for (int i = 0; i < NrOfLights; i++)
            {
                Group[i] = new LightControls();
                Group[i].setMyLight(J.getInStreets()[i].getMyLight());
            }
        }
        //public void countTOSI1(double x)
        //{
        //    WaitTimeSum += (-((Math.Pow(x, 1.6) * 100) / Math.Pow(myJunction.getMaxCars(), 1.6)) + 100);
        //}
        //public void countTOSI2(double x)
        //{
        //    WaitTimeAvg += (-((Math.Pow(x, 1.6) * 100) / Math.Pow(20, 1.6)) + 100);
        //}
        //public void countTOSI3(double x)
        //{
        //    JunctionCongestion += (-((Math.Pow(x, 1.6) * 100) / Math.Pow(100, 1.6)) + 100);
        //}
        //public void countTOSI4(double x)
        //{
        //    StreetLoad += (-((Math.Pow(x, 1.6) * 100) / Math.Pow(100, 1.6)) + 100);
        //}
        public void avgTOSI()
        {
            waitingCarsAvg /= Var.Intervals;
            waitingTimeAvg /= Var.Intervals;
            junctionCongestion /= Var.Intervals;
        }
        public void countTOSI()
        {
            for (int i = 0; i < Group.Length; i++)
            {
                sum1 += Group[i].getWaitingCars();
                sum2 += Group[i].getWaitingTime();
                sum3 += Group[i].getStreetLoad();
            }
            sum1 /= Group.Length;
            sum2 /= Group.Length;
            sum3 /= Group.Length;
            avgTOSI();
            TOSI = (waitingCarsAvg + waitingTimeAvg + junctionCongestion + sum1 + sum2 + sum3) / 6;
        }
        public void setTo(LightGroup LG)
        {
            for (int i = 0; i < LG.Group.Length; i++)
            {
                Group[i].setLightControls(LG.Group[i].getLightControls());
            }
        }

        //public LightGroup(ref LightGroup LG)
        //{
        //    WaitTimeSum = LG.WaitTimeSum;
        //    WaitTimeAvg = LG.WaitTimeAvg;
        //    JunctionCongestion = LG.JunctionCongestion;
        //    StreetLoad = LG.StreetLoad;
        //    TOSI = LG.TOSI;
        //    myJunction = LG.myJunction;
        //    Group = new List<LightControls>();
        //    for (int i = 0; i < (LG.Group.Count); i++)
        //    {
        //        LightControls LC = LG.Group[i];
        //        Group.Add(LC);
        //    }
        //}

        //public void setLightGroup(ref LightGroup LG)
        //{
        //    Group = LG.Group;
        //    myJunction = LG.myJunction;
        //    WaitTimeAvg = LG.WaitTimeAvg;
        //    WaitTimeSum = LG.WaitTimeSum;
        //    JunctionCongestion = LG.JunctionCongestion;
        //    StreetLoad = LG.StreetLoad;
        //    TOSI = LG.TOSI;
        //}

        //public Junction getGroupJunction()
        //{
        //    return myJunction;
        //}

        //public void setLightGroup(List<double[]> L)
        //{
        //    for (int i = 0; i < L.Count; i++)
        //        Group[i].setLightControls(L[i]);
        //}

        //public List<double[]> getLightGroupControls()
        //{
        //    List <double[]> tempLightGroup = new List<double[]>();
        //    for (int i = 0; i < Group.Length; i++)
        //        tempLightGroup.Add(Group[i].getLightControls());
        //    return tempLightGroup;
        //}

        //public LightGroup[] getLightGroup()
        //{
        //    return Group;
        //}

        public void countWaitingCarsAvg(double x)
        {
            waitingCarsAvg += 100 * Math.Exp(-5*x/myJunction.getMaxCars());
        }
        public void countWaitingTimeAvg(double x)
        {
            waitingTimeAvg += 100 * Math.Exp(-5*x/Var.Iterations);
        }
        public void countJunctionCongestion(double x)
        {
            junctionCongestion += 100 * Math.Exp(-5*x/100);
        }
        public double getWaitingCarsAvg()
        {
            return waitingCarsAvg;
        }
        public double getWaitingTimeAvg()
        {
            return waitingTimeAvg;
        }
        public double getJunctionCongestion()
        {
            return junctionCongestion;
        }
        public void setGroupTOSI(double T)
        {
            TOSI = T;
        }
        public double getGroupTOSI()
        {
            return TOSI;
        }
    }
}
